﻿#region Using Statements
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;
using System.Collections.Generic;
using System;
using Microsoft.Xna.Framework.Input;
#endregion

namespace pigs
{
    /// <summary>
    /// The options screen is brought up over the top of the main menu
    /// screen, and gives the user a chance to configure the game
    /// in various hopefully useful ways.
    /// </summary>
    class HighScoreScreen : MenuScreen
    {

        ContentManager content;
        SpriteFont font;
        HighScores hs;
        HighScoreEntry[] currentScoreList;
        MenuEntry currentLevelEntry;
        String currentLevelName;

        /// <summary>
        /// Constructor.
        /// </summary>
        public HighScoreScreen(ScreenManager screen, HighScores scores)
            : base("High Scores")
        {
            hs = scores;

            MenuEntry back = new MenuEntry("Back");
            back.Position = new Vector2(screen.GraphicsDevice.Viewport.Width / 2, 800);
            back.Selected += OnCancel;

            currentLevelName = "Ontario";
            currentLevelEntry = new MenuEntry("Scores for " + currentLevelName);
            currentScoreList = hs.ontScore;
            currentLevelEntry.Selected += currentLevelEntrySelected;

            MenuEntries.Add(currentLevelEntry);
            MenuEntries.Add(back);
        }

        void currentLevelEntrySelected(object sender, PlayerIndexEventArgs e)
        {
            if (currentLevelName.Equals("Survival"))
            {
                currentLevelName = "Ontario";
                currentScoreList = hs.ontScore;
            }
            else if (currentLevelName.Equals("Ontario"))
            {
                currentLevelName = "Saskatchewan";
                currentScoreList = hs.skScore;
            }
            else if (currentLevelName.Equals("Saskatchewan"))
            {
                currentLevelName = "British Columbia";
                currentScoreList = hs.bcScore;
            }
            else if (currentLevelName.Equals("British Columbia"))
            {
                currentLevelName = "Survival";
                currentScoreList = hs.surScore;
            }
            currentLevelEntry.Text = "Scores for " + currentLevelName;

        }


        public override void LoadContent()
        {
            if (content == null)
                content = new ContentManager(ScreenManager.Game.Services, "Content");

            this.font = content.Load<SpriteFont>("fonts/menufont");
        }

        /// <summary>
        /// Draws the loading screen.
        /// </summary>
        public override void Draw(GameTime gameTime)
        {

            base.Draw(gameTime);

            SpriteBatch spriteBatch = ScreenManager.SpriteBatch;

            // Draw the text.
            spriteBatch.Begin();

            drawScores(spriteBatch, currentScoreList);

            spriteBatch.End();
        }

        public void drawScores(SpriteBatch spriteBatch, HighScoreEntry[] scores)
        {
            Viewport viewport = ScreenManager.GraphicsDevice.Viewport;
            Vector2 viewportSize = new Vector2(viewport.Width, viewport.Height);
            Color color = Color.White * TransitionAlpha * TransitionAlpha;

            for (int i = 0; i < scores.Length; i++)
            {
                if (i != 9)
                {
                    spriteBatch.DrawString(font, (i + 1).ToString() + ".  " + scores[i].name, new Vector2(3 * viewport.Width / 7 - 25, 235 + 50 * i), color);
                }
                else
                {
                    spriteBatch.DrawString(font, (i + 1).ToString() + ". " + scores[i].name, new Vector2(3 * viewport.Width / 7 - 25, 235 + 50 * i), color);
                }
                spriteBatch.DrawString(font, scores[i].score.ToString(), new Vector2(4 * viewport.Width / 7 + 25, 235 + 50 * i), color);
            }
        }
    }
}
